var cfg = {
    baseUrl: "http://47.95.255.150:8888"
};

var tip = (function() {
    toastr.options = {
        closeButton: true,
        debug: false,
        newestOnTop: true,
        progressBar: true,
        positionClass: "toast-top-center",
        preventDuplicates: false,
        onclick: null,
        showDuration: "300",
        hideDuration: "1000",
        timeOut: "6000",
        extendedTimeOut: "1000",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut"
    };

    return {
        info: toastr.info,
        succ: toastr.success,
        warn: toastr.warning,
        err: toastr.error
    };
})();

function getStr(v) {
    if (v) {
        if (v != "") return v;
    }
    return "";
}

function getAdd(v) {
    if (v) {
        if (v == "") return ["", ""];
        if (v.split("-").length == 2) return v.split("-");
        return [v, ""];
    }
    return ["", ""];
}

function getNum(v) {
    if (v) {
        if (isNaN(v)) {
            return 0;
        }
        return parseFloat(v);
    }
    return 0;
}

function getDayStr(offset) {
    var o = offset * 1000 * 60 * 60 * 24;

    var t = new Date().getTime();

    var d = new Date(t + o);

    var y = d.getFullYear();
    var m = d.getMonth() + 1 > 9 ? (d.getMonth() + 1).toString() : "0" + (d.getMonth() + 1);
    var day = d.getDate() > 9 ? d.getDate().toString() : "0" + d.getDate();

    return y + "-" + m + "-" + day;
}
function getDayTimeStr(t) {
    var date = new Date(t);
    var yyyy = date.getFullYear().toString();
    var mm = date.getMonth() + 1 > 9 ? (date.getMonth() + 1).toString() : "0" + (date.getMonth() + 1);
    var dd = date.getDate() > 9 ? date.getDate().toString() : "0" + date.getDate();

    var h = date.getHours() > 9 ? date.getHours().toString() : "0" + date.getHours();
    var m = date.getMinutes() > 9 ? date.getMinutes().toString() : "0" + date.getMinutes();
    var s = date.getSeconds() > 9 ? date.getSeconds().toString() : "0" + date.getSeconds();

    return yyyy + "-" + mm + "-" + dd + " " + h + ":" + m;
}
function getDayTime(tt) {
    var d = new Date();
    var y = d.getFullYear();
    var m = d.getMonth() + 1;
    var day = d.getDate();

    var nd = new Date(y + "-" + m + "-" + day + " " + tt);
    return nd.getTime();
}

function isWX() {
    var ua = window.navigator.userAgent.toLowerCase();
    return ua.match(/MicroMessenger/i) == "micromessenger";
}

var ajax = (function() {
    function send(url, ajaxName) {
        return function(arg) {
            var fullurl = url;

            var opt = {
                url: fullurl,
                type: "post",
                dataType: "json",
                xhrFields: {
                    withCredentials: true
                }
            };

            if (Object.prototype.toString.call(arg.param) == "[object FormData]") {
                opt.data = arg.param;
                // opt.cache = false;
                opt.processData = false;
                opt.contentType = false;
            } else {
                opt.data = JSON.stringify(arg.param ? arg.param : {});
                // opt.data = arg.param;
            }

            $.ajax(opt)
                .done(function(data) {
                    if (data.status_code == 200) {
                        arg.cb(data);
                    } else {
                        arg.failCb ? arg.failCb(data) : tip.err(data.info);
                    }
                })
                .fail(function(jqXHR, textStatus, errorThrown) {
                    // console.log(jqXHR, textStatus, errorThrown);
                    arg.failCb ? arg.failCb(data) : tip.err((ajaxName ? ajaxName : "url") + " 失败");
                });
        };
    }

    return {
        get_list: send(cfg.baseUrl + "/api/list", "产品列表"),
        get_code: send(cfg.baseUrl + "/api/code", "验证码"),
        register: send(cfg.baseUrl + "/api/register", "注册"),
        agreement: send(cfg.baseUrl + "/api/agreement", "用户协议")
    };
})();
